package com.serkol.ams.config;


import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.v3.oas.models.*;
import io.swagger.v3.oas.models.annotations.OpenAPI31;
import io.swagger.v3.oas.models.headers.Header;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.HeaderParameter;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;

import org.springdoc.core.customizers.OpenApiCustomizer;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collection;
import java.util.List;

@Configuration
@EnableKnife4j
@OpenAPI31
public class SwaggerConfig {
    @Bean
    public OpenAPI customOpenAPI() {
   /*     SecurityRequirement securityItem = new SecurityRequirement();
        securityItem.addList("Authorization");
        return new OpenAPI().components(new Components().addSecuritySchemes("basicScheme", new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("basic"))
                        .addParameters("myHeader1", new Parameter().in("header").schema(new StringSchema()).name("myHeader1")).addHeaders("myHeader2", new Header().description("myHeader2 header").schema(new StringSchema()))).addSecurityItem(securityItem)
                .info(new Info()
                        .title("代理管理系统")
                        .description("系统信息接口")
                        .version("v1.0.0"));*/
         return new OpenAPI()
                .addSecurityItem(new SecurityRequirement().addList("bearerAuth"))
                .components(new Components()
                        .addSecuritySchemes("bearerAuth", new SecurityScheme()
                                .type(SecurityScheme.Type.HTTP)
                                .scheme("bearer")
                                .bearerFormat("JWT")));
    }

    public OpenApiCustomizer customerGlobalHeaderOpenApiCustomiser() {
      //  log.info("[系统启动][SwaggerUI]initBean: customerGlobalHeaderOpenApiCustomiser");
        return new OpenApiCustomizer(){
            @Override
            public void customise(OpenAPI openApi) {
                Paths paths = openApi.getPaths();
                //log.info("[系统启动][SwaggerUI]paths="+paths.toString());
                Collection<PathItem> values = paths.values();
                for (PathItem pathItem : values) {
                    List<Operation> operations = pathItem.readOperations();
                    //
                    for (Operation operation : operations) {
                        HeaderParameter headerParameter = new HeaderParameter();
                        //headerParameter.setRequired(true);
                        headerParameter.setName("Authorization");
                        operation.addParametersItem(headerParameter);
                    }
                }
            }
        };
    }

    @Bean
    public GroupedOpenApi userOpenApi() {
        String[] packagesToscan = {"com.serkol.ams.module.sys.controller","com.serkol.ams.module.base"};
        return GroupedOpenApi.builder().group("users").addOpenApiCustomizer(customerGlobalHeaderOpenApiCustomiser()).packagesToScan(packagesToscan)
                .build();
    }



}
